import { _decorator, clamp, Component, Node } from 'cc';
import { Globals } from './Globals';
import { Constant } from './Constant';
import { SkillCard } from './SkillCard';
import { Skill, SkillFireBall, SkillInvincelble, SkillRoll } from './Skill';
import { BattleContext } from './BattleContext';
const { ccclass, property } = _decorator;

@ccclass('SkillSelectPanel')
export class SkillSelectPanel extends Component {

    @property(Node) ndSkills:Node;



    protected onEnable(): void {
        this._showCards();
    }

    protected onDisable(): void {
        
    }

    start() {
        // // TODO TEST
        // let skills: Skill[] = [];
        // skills.push(new SkillFireBall());
        // skills.push(new SkillFireBall());
        // skills.push(new SkillFireBall());

        // this._createSkillCards(skills);
    }

    update(deltaTime: number) {
        
    }

    private _showCards() {
        let sks:Skill[] = [];

        sks.push(new SkillRoll());
        sks.push(new SkillInvincelble())
        sks.push(new SkillFireBall());

        this._createSkillCards(sks);
    }


    private _createSkillCards(skills: Skill[]) {
        
        // 安全检查
        if(!skills || skills.length === 0) {
            return
        }

        const len = skills.length;

        for( let i = 0; i < len; i++) {
            const sk = skills[i];
            const ndCard = Globals.getNode(Constant.PrefabUrl.SKILL_CARD, this.ndSkills);
            const card = ndCard.getComponent(SkillCard);
            card.setSkill(skills[i]);
            card.onClick(()=> {
                BattleContext.player.learnSkill(sk);
                this.node.active = false;
                // TODO player 学习技能
            }, this);
        }

        const cardWidth = 200; // TODO 需要从prefab中获取,动态获取
        const w = 20; // 卡片之间的间距
        const y = 0; // 卡片在y轴上的位置

        const totalWidth = len * cardWidth + (len - 1) * w;
        const half = totalWidth / 2;

        const startX = -half + cardWidth / 2;

        for( let i = 0; i < len; i++) {
            const x = startX + i * (cardWidth + w);
            this.ndSkills.children[i].setPosition(x, y);
        }



    }






}


